<?php

class Music extends AppModel {

    var $alias = 'Music';
    var $name = 'Music';
    var $useTable = 'musics';
    var $belongsTo = array(
        'Singer' => array(
            'className' => 'Singer',
            'foreignKey' => 'singer_id',
            'fields' => array('Singer.id', 'Singer.title')
        ),
        'music_style' => array(
            'className' => 'music_style',
            'foreignKey' => 'style_id',
            'fields' => array('music_style.id', 'music_style.title')
        )
    );

    function delete($id = null, $cascade = true) {
        App::import('Component', 'Upload');
        $this->id = $id;
        $upload = new UploadComponent();

        //delete thumbnail
        $thumbnail = $this->field('thumbnail');
        if (is_file(Configure::read('music_thumb_absolute_path') . $thumbnail) && $thumbnail != 'no_image.gif') {
            $upload->delete(Configure::read('music_thumb_absolute_path') . $thumbnail);
        }
        //delete video file
        $thumbnail = $this->field('url');
        if (is_file(Configure::read('music_absolute_path') . $thumbnail) && $thumbnail != 'no_image.gif') {
            $upload->delete(Configure::read('music_absolute_path') . $thumbnail);
        }

        parent::delete($id, $cascade);
    }

    function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
        $parameters = compact('conditions');
        $this->recursive = $recursive;
        $count = $this->find('count', array_merge($parameters, $extra));
        if (isset($extra['group'])) {
            $count = $this->getAffectedRows();
        }
        return $count;
    }

    function writeHomeMusics($limit = 6) {
        $data = $this->find('all', array(
                    'conditions' => array('Music.published' => '1', 'Music.hot_music' => '1'),
                    'fields' => array('Music.id', 'Music.alias', 'Music.title', 'Music.thumbnail', 'Singer.title'),
                    'order' => 'Music.id DESC',
                    'limit' => $limit,
                ));

        $html = '<div id="gamethoitrang"><div class="gamethoitrang1">
          <h3>THỜI TRANG VÀ ÂM NHẠC</h3>
        </div>' . "\n";
        foreach ($data AS $row) {
            $html .= '<ul class="gamethoitrang2"><li><a href="/nhac/chi-tiet/' .
                    $row['Music']['id'] . '-' . $row['Music']['alias'] . '.html">
                  <img src="' . Configure::read('music_thumb_url_path') . $row['Music']['thumbnail'] .
                    '" /></a></li><li><a href="/nhac/chi-tiet/' .
                    $row['Music']['id'] . '-' . $row['Music']['alias'] . '.html">' . $row['Music']['title'] . '</a></li>
                    <li><span>' . $row['Singer']['title'] . '</span></li>
                  </ul>';
        }
        $html .= '<p>
            <a href="/nhac.html">Xem thêm</a>
            </p></div>';
        $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'home_nhac.ctp', 'w');
        fwrite($resourse, $html);
    }

}

?>
